
package com.sawgdev.android.bokuiki.common.mail;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

/**
 * <pre>
 * 暗号化を行うためのクラスです。
 * </pre>
 *
 * @author Bambooroid
 */
public class Encipherment {

    private static final String KEY = "ijoijgavknva";

    /**
     * <pre>
     * パスワードを暗号化します
     * </pre>
     *
     * @param text 暗号化するパスワード
     * @return 暗号化したパスワード
     * @throws javax.crypto.IllegalBlockSizeException
     * @throws java.security.InvalidKeyException
     * @throws java.security.NoSuchAlgorithmException
     * @throws java.io.UnsupportedEncodingException
     * @throws javax.crypto.BadPaddingException
     * @throws javax.crypto.NoSuchPaddingException
     */
    public String encrypt(String text) throws IllegalBlockSizeException, InvalidKeyException,
            NoSuchAlgorithmException, UnsupportedEncodingException, BadPaddingException,
            NoSuchPaddingException {
        // 鍵を生成
        SecureRandom secureRandom = new SecureRandom(Base64.decode(KEY, Base64.DEFAULT));
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(secureRandom);
        SecretKey secretKey = keyGenerator.generateKey();

        // 暗号化
        byte[] clearTextByte = text.getBytes();
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(clearTextByte);
        return new String(Base64.encode(encryptedBytes, Base64.DEFAULT));
    }

    /**
     * <pre>
     * パスワードを複合化します。
     * </pre>
     *
     * @param text 暗号化されたパスワード
     * @return 複合化したパスワード
     * @throws javax.crypto.IllegalBlockSizeException
     * @throws java.security.InvalidKeyException
     * @throws java.security.NoSuchAlgorithmException
     * @throws java.io.UnsupportedEncodingException
     * @throws javax.crypto.BadPaddingException
     * @throws javax.crypto.NoSuchPaddingException
     */
    public String decrypt(String text) throws IllegalBlockSizeException, InvalidKeyException,
            NoSuchAlgorithmException, UnsupportedEncodingException, BadPaddingException,
            NoSuchPaddingException {
        // 鍵を生成
        SecureRandom secureRandom = new SecureRandom(Base64.decode(KEY, Base64.DEFAULT));
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(secureRandom);
        SecretKey secretKey = keyGenerator.generateKey();

        // 複合化
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedByte = cipher.doFinal(Base64.decode(text, Base64.DEFAULT));
        String decryptedText = new String(decryptedByte);
        return decryptedText;
    }
}
